package com.android.demo;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.ActivityGroup;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.demo.widget.BitmapToolkit;
import com.android.demo.widget.HttpToolkit;
import com.android.demo.widget.takeacay.ProductInfo;
import com.android.demo.widget.takeacay.ProductSdk;

public class TestRect extends ActivityGroup {
	LinearLayout frameMain;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		frameMain = new LinearLayout(this);
		frameMain.setBackgroundColor(Color.RED);
		setContentView(frameMain);
	
		frameMain.setScrollContainer(true);
		frameMain.setOrientation(LinearLayout.VERTICAL);
		
//		loadAndPostBangBang();
//		loadAndPostTemplate();
		loadAndPostMySelf();
	}
	
	private void loadAndPostMySelf(){
		
		ArrayList<ProductInfo> list = new ArrayList<ProductInfo>();
		//"许多人都特别喜欢“冰凝城夏”的名字，把这个城市的夏天凝结在冰淇淋上，一口一口吃掉。冰淇淋的口味品种很多，底托很脆，价格也比较亲民。和其他冰淇淋店不同，冰凝城夏采取的是外卖式销售，但凡在繁华的街头，你几乎都能找到它。逛街逛累了，只需要站到冰凝城夏的橱窗前，点上两球你喜欢的口味，解解暑是很不错的。可惜的是，只提供外卖，不可以停下来歇脚。"
		
		list.add(new ProductInfo(26.083073, 119.300537, "冰凝城夏（安泰店）", "87661096","许多人都特别喜欢“冰凝城夏”的名字，把这个城市的夏天凝结在冰淇淋上，一口一口吃掉。"));
		list.add(new ProductInfo(26.036039,119.310837, "冰凝城夏（学生街店）", "83165608","许多人都特别喜欢“冰凝城夏”的名字，把这个城市的夏天凝结在冰淇淋上，一口一口吃掉。"));
//		list.add(new ProductInfo(26.083073, 119.300537, "冰凝城夏（宝龙店）", "83810336","许多人都特别喜欢“冰凝城夏”的名字，把这个城市的夏天凝结在冰淇淋上，一口一口吃掉。"));
		list.add(new ProductInfo(26.051231,119.317703, "冰凝城夏（附中店）", "83537977","许多人都特别喜欢“冰凝城夏”的名字，把这个城市的夏天凝结在冰淇淋上，一口一口吃掉。"));
		
		mList = list;
		ProductSdk sdk = new ProductSdk();
		for (int i=0;i<mList.size();i++){
			ProductInfo info = mList.get(i);
			BitmapToolkit bt = new BitmapToolkit(BitmapToolkit.DIR_MYHOME_TAKEAWAY, info.imageUrl, "", ".jpg");
			byte[] dd = bt.loadBytesNetOrLocal();
			if (dd==null) {
				info.imageUrl="";
			}
			else {
				info.imageUrl = bt.getAbsolutePath();
			}
			
			sdk.postShop(info);
			
			final int index = i;
			mHandler.post(new Runnable() {
				@Override
				public void run() {
					TextView textview = (TextView)(frameMain.getChildAt(0));
					try {
						
						textview.setText("当前下载"+index+"/"+mList.size());
					}catch (Exception e) {
					}
				}
			});
		}
	}
	
	private Handler mHandler = new Handler();
	private void loadAndPostBangBang(){
		mList= new ArrayList<ProductInfo>();
		new Thread(){
			public void run(){
				for (int i=1;i<5;++i) {
					HttpToolkit http = new HttpToolkit("http://www.shishi.jqw.com/11304/life-"+i+".htm");
					http.setEnc("GB2312");
					http.DoGet();
					decodeBangBang(http.GetResponse().replace("\n","").replace("\t","").replace("\r",""));
				}
				
				ProductSdk sdk = new ProductSdk();
				for (int i=0;i<mList.size();i++){
					ProductInfo info = mList.get(i);
					BitmapToolkit bt = new BitmapToolkit(BitmapToolkit.DIR_MYHOME_TAKEAWAY, info.imageUrl, "", ".jpg");
					byte[] dd = bt.loadBytesNetOrLocal();
					if (dd==null) {
						info.imageUrl="";
					}
					else {
						info.imageUrl = bt.getAbsolutePath();
					}
					
					sdk.postShop(info);
					
					final int index = i;
					mHandler.post(new Runnable() {
						@Override
						public void run() {
							TextView textview = (TextView)(frameMain.getChildAt(0));
							try {
								
								textview.setText("当前下载"+index+"/"+mList.size());
							}catch (Exception e) {
							}
						}
					});
				}
			}
		}.start();
	}
	
	private void loadAndPostTemplate(){ 
		mList= new ArrayList<ProductInfo>();
		
		new Thread(){
			public void run(){
				
				decodeTemplate();
				
				ProductSdk sdk = new ProductSdk();
				for (int i=0;i<mList.size();i++){
					ProductInfo info = mList.get(i);
					
					sdk.postShop(info);
					
					final int index = i;
					mHandler.post(new Runnable() {
						@Override
						public void run() {
							TextView textview = (TextView)(frameMain.getChildAt(0));
							try {
								
								textview.setText("当前下载"+index+"/"+mList.size());
							}catch (Exception e) {
							}
						}
					});
				}
			}
		}.start();
	}
	
	private ArrayList<ProductInfo> mList; 
	private String decodeBangBang(String src){
		String patternStr= "href=\"(?:[^\"]+)\"><img src='([^']+)' width='100' height='85'><\\/a><\\/td><td>     <td colspan=\"5\" valign=\"top\" class=\"cd\"><div>(?:<span style=\"float:right;\"><a target='_blank' class=\"rz\" href='([^']+)'>进入网站<\\/a><\\/span>)?<a  target='_blank'  href=\"(?:[^\"]+)\">([^<]+)<\\/a><\\/div><span style=\"color:red;\">(?:[^<]+)<\\/span>(?:[^<]+)<div>([^<]*)<\\/div><span style='color:blue;'>电话:([^<]+)<\\/span><br><span style='color:blue;'>地址:([^<]+)<\\/span>";
		
		Pattern pattern = Pattern.compile(patternStr, Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
		Matcher matcher = pattern.matcher(src);
		while (matcher.find()) {
			//String s=matcher.group();
			// image
			String s1=matcher.group(1);
			// network station
			String s2=matcher.group(2);
			// shop name
			String s3=matcher.group(3);
			// descript
			String s4=matcher.group(4);
			// telephone
			String s5=matcher.group(5);
			// address
			String s6=matcher.group(6);
			
//			if (s2!=null) continue;
			
			final ProductInfo info = new ProductInfo();
			info.imageUrl = s1;
			info.name = s3;
			info.des = s4+"\n"+s6;
			s5 = s5.replace("—", "");
			s5 = s5.replace("-", "");
			String []phonesMess = s5.split(" ");
			ArrayList<String> strPhones =new ArrayList<String>();
			
			for (String phoneMess : phonesMess) {
				if (phoneMess.length()<5 ) continue;
				
				boolean isRepeat = false;
				for (String strPhone : strPhones) {
					if (phoneMess.equals(strPhone)){
						isRepeat = true;
					}
				}
				if (!isRepeat) {
					strPhones.add(phoneMess);
					Log.i("TestRest", s5+" ---- "+phoneMess);
				}
			}
			for (String phone:strPhones) {
				info.phone+=phone+" ";
			}
			
			info.address = s6;
			info.latitude=24.734749;
			info.longitude = 118.63904;
			mList.add(info);
			
			final String strContent = "数："+mList.size() + " 容" +s1+ " " +s2+ " " +s3+ " " +s4+ " " +s5;
			mHandler.post(new Runnable() {
				@Override
				public void run() {
					TextView textview1 = new TextView(TestRect.this);
					frameMain.addView(textview1, 0);
					try {
						textview1.setSingleLine(true);
						textview1.setText(strContent);
					}catch (Exception e) {
					}
				}
			});
		}
		return "";
	}
	
	private void decodeTemplate(){
		String src = getString(R.string.takeaway);
		String patternStr = "([\\S]*)[\\s]+([\\S]*)[\\s]+([\\S]*)——([\\S]*)[\\s]+([\\S]*)"; 
		
		Pattern pattern = Pattern.compile(patternStr);
		Matcher matcher = pattern.matcher(src);
		
		while (matcher.find()) {
			String s=matcher.group();
			// name
			String s1=matcher.group(1);
			// phone
			String s2=matcher.group(2);
			// 时间
			String s3=matcher.group(3);
			String s4=matcher.group(4);
			// 地址
			String s5=matcher.group(5);
			
			final ProductInfo info = new ProductInfo();
			info.name = s1;
			info.phone = s2;
			info.des = s5;
			info.address = s5;
			info.latitude=24.734749;
			info.longitude = 118.63904;
			mList.add(info);
			
			final String strContent = "数："+mList.size() + " 容" +s1+ " " +s2+ " " +s3+ " " +s4+ " " +s5;
			mHandler.post(new Runnable() {
				@Override
				public void run() {
					TextView textview1 = new TextView(TestRect.this);
					frameMain.addView(textview1, 0);
					try {
						textview1.setSingleLine(true);
						textview1.setText(strContent);
					}catch (Exception e) {
					}
				}
			});
		}
	}
	
//	private String html2normal(String src){
//		String patternStr = "<A href=\"momo://user=([^\"]*?)\">([^<]*?)</A>";
//		
//		Pattern pattern = Pattern.compile(patternStr);
//		Matcher matcher = pattern.matcher(src);
//		while (matcher.find()) {
//			String s=matcher.group();
//			long uid = Long.valueOf(matcher.group(1));
//			String name = matcher.group(2);
//			src = matcher.replaceFirst(name);
//			matcher = pattern.matcher(src);	
//		}
//		return src;
//	}
}